package com.smartHire.controller.Business;

import com.smartHire.domain.Goods;
import com.smartHire.domain.Order;
import com.smartHire.domain.Order_goods;
import com.smartHire.service.GoodsService;
import com.smartHire.service.OrderService;
import com.smartHire.service.Order_goodsService;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;

@WebServlet(name = "SendOutServlet", value = "/SendOutServlet")
public class SendOutServlet extends HttpServlet{
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        String o_id = request.getParameter("o_id");
        int good_id= Integer.parseInt(request.getParameter("good_id"));
        int number= Integer.parseInt(request.getParameter("number"));


        WebApplicationContext ctx = null;
        //使用框架中的方法，获取容器对象
        ServletContext sc = getServletContext();
        ctx = WebApplicationContextUtils.getRequiredWebApplicationContext(sc);
        System.out.println("容器对象的信息========"+ctx);

        try {
            OrderService orderService = (OrderService) ctx.getBean("orderService");
            GoodsService goodsService=(GoodsService) ctx.getBean("goodsService");

            Order order = new Order();
            order.setOrder_Id(o_id);
            order = orderService.queryOrderByOrder_Id(order);

            Goods goods=new Goods();
            goods.setGood_id(good_id);
            goods = goodsService.queryGoodsByID(goods);


            order.setStatus(1);

            if(orderService.updateOrderStatus(order)==1)
            {
                number = goods.getInventory()-number;
                goods.setInventory(number);
                if(goodsService.updateGoodsInventory(goods)==1){
                    response.getWriter().write("OK");
                }

            }

        }catch (Exception e)
        {
            e.printStackTrace();
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

}
